package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductBuyLimit;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsProductBuyLimit.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddDirectMailGoodsProductBuyLimitDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditDirectMailGoodsProductBuyLimitDTO;
import com.ecoolex.chinadutyfree.retailer.ws.DirectMailGoodsProductBuyLimitService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.Admin, description = "直邮产品限购管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
    key = DirectMailGoodsProductBuyLimitGraphql.AUTH,
    name = "直邮产品限购管理")
public class DirectMailGoodsProductBuyLimitGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-DIRECT-MAIL-GOODS-PRODUCT-BUY-LIMIT";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@Reference
	private DirectMailGoodsProductBuyLimitService directMailGoodsProductBuyLimitService;
	
	@QueryField(field = "getDirectMailGoodsProductBuyLimitByDirectMailProductId", description = { "根据直邮产品Id查询限购详情" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "根据直邮产品Id查询限购详情")
	public DirectMailGoodsProductBuyLimit getByProductId(@NonNull Integer directMailProductId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return directMailGoodsProductBuyLimitService.getByProductId(directMailProductId);
	}
	
	@MutationField(field = "addDirectMailGoodsProductBuyLimit", description = "添加直销产品限购")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加直销产品限购")
	public DirectMailGoodsProductBuyLimit add(@NonNull AddDirectMailGoodsProductBuyLimitDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return directMailGoodsProductBuyLimitService.add(record, user.userName());
	}
	
	@MutationField(field = "editDirectMailGoodsProductBuyLimit", description = "编辑直销产品限购")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑直销产品限购")
	public DirectMailGoodsProductBuyLimit edit(@NonNull EditDirectMailGoodsProductBuyLimitDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		DirectMailGoodsProductBuyLimit limit = directMailGoodsProductBuyLimitService.get(record.getId());
		Assert.isTrue(Check.notNull(limit),ResultCode.VERIFY.build(1), "产品限购策略不存在");
		BeanUtil.copy(record, limit,
			Lists.newArrayList(Column.deleted, Column.createUser, Column.createTime).stream().map(
				Column::getJavaProperty).toArray(String[]::new));
		return directMailGoodsProductBuyLimitService.edit(limit, user.userName());
	}
	
	@MutationField(field = "delDirectMailGoodsProductBuyLimit", description = "删除直销产品限购")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除直销产品限购")
	public Boolean del(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		DirectMailGoodsProductBuyLimit limit = directMailGoodsProductBuyLimitService.get(id);
		Assert.isTrue(Check.notNull(limit),ResultCode.VERIFY.build(1), "产品限购策略不存在");
		directMailGoodsProductBuyLimitService.delete(limit, user.userName());
		return true;
	}
}
